package com.tlcx.kfip.fragment.main;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ListView;

import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.loadmore.SwipeRefreshHelper;
import com.tlcx.kfip.R;
import com.tlcx.kfip.adapter.AlreadyAnnouncedAdapter;
import com.tlcx.kfip.entity.IpRecordAlreadyEntity;
import com.tlcx.kfip.fragment.base.BaseFragment;
import com.tlcx.kfip.utils.Constants;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 夺宝记录 已揭晓
 * Created by wanjixin on 2016/10/14.
 */

public class IPRecordsAleadyAnnouncedFragment extends BaseFragment {

    @BindView(R.id.refresh_layout)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.lv_refresh)
    ListView mListView;

    private int mPage = 1;                                            //加载页数
    private Handler mHandler;
    private Unbinder unbinder;
    private AlreadyAnnouncedAdapter mAdapter;
    private SwipeRefreshHelper mSwipeRefreshHelper;

    @Override
    protected int getContentView() {
        return R.layout.frag_ip_records_ongoing;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);

        mHandler = new Handler();
        mAdapter = new AlreadyAnnouncedAdapter(getActivity());

        //设置适配器
        mListView.setAdapter(mAdapter);

        //设置刷新控件的颜色
        mRefreshLayout.setColorSchemeColors(Color.RED,Color.BLUE,Color.GREEN,Color.YELLOW);

        mSwipeRefreshHelper = new SwipeRefreshHelper(mRefreshLayout);

        //设置自动刷新
        mRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshHelper.autoRefresh();
            }
        });

        //设置下拉手动刷新
        mSwipeRefreshHelper.setOnSwipeRefreshListener(new SwipeRefreshHelper.OnSwipeRefreshListener() {
            @Override
            public void onfresh() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadFirstPageData();
                    }
                },1000);

            }
        });

        //设置上拉加载更多
        mSwipeRefreshHelper.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPage ++;
                        loadNextPageData(mPage);
                    }
                },1000);
            }
        });

    }

    /**
     * 获得第一页数据
     */
    private void loadFirstPageData(){
        mPage = 1;
        mAdapter.clearData();
        List<IpRecordAlreadyEntity> data = makeMockData();
        mAdapter.addAllAndNotify(data);
        mSwipeRefreshHelper.refreshComplete();          //刷新完成
        if (data.size()>= Constants.GRID_LOAD_COUNT){
            mSwipeRefreshHelper.setLoadMoreEnable(true);//设置可加载更多
        }
    }

    /**
     * 加载第page页数据
     * @param page
     */
    private void loadNextPageData(int page){
        List<IpRecordAlreadyEntity> data = makeMockData();
        mAdapter.addAllAndNotify(data);
        mSwipeRefreshHelper.loadMoreComplete(true);//设置加载完成
    }
    /**
     * 造假数据
     */
    private List<IpRecordAlreadyEntity> makeMockData(){
        List<IpRecordAlreadyEntity> data = new ArrayList<>();
        IpRecordAlreadyEntity entity1 = new IpRecordAlreadyEntity(1,R.mipmap.mock_jingdong_1,"京东E卡经典卡200元面值【10元秒开】",800,700,"呵呵呵呵","00000009","2016-11-14");
        IpRecordAlreadyEntity entity2 = new IpRecordAlreadyEntity(2,R.mipmap.mock_jingdong_2,"京东E卡经典卡200元面值【10元秒开】",1800,600,"嘿嘿嘿","00000004","2016-12-11");
        IpRecordAlreadyEntity entity3 = new IpRecordAlreadyEntity(3,R.mipmap.mock_qq_coin,"30元Q币 自动充值 即时到账",50,49,"嚯嚯嚯","00000003","2016-10-11");
        IpRecordAlreadyEntity entity4 = new IpRecordAlreadyEntity(4,R.mipmap.mock_phone_fee,"全国通用 中国移动/电信 5元话费（联通除外）",340,120,"咩咩咩","00000006","2016-06-17");
        IpRecordAlreadyEntity entity5 = new IpRecordAlreadyEntity(5,R.mipmap.mock_car_1,"2017最新款奔驰GLK500纯金色打造",888800,700,"哞哞哞","00000007","2016-08-27");
        IpRecordAlreadyEntity entity6 = new IpRecordAlreadyEntity(6,R.mipmap.mock_car_2,"宝马2017款320Li时尚型",600,500,"汪汪汪","00000009","2016-08-28");

        data.add(entity1);
        data.add(entity2);
        data.add(entity3);
        data.add(entity4);
        data.add(entity5);
        data.add(entity6);

        return data;
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();

    }
}
